package com.flot.yazb.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.flot.yazb.R;
import com.flot.yazb.base.BActivity;

import com.flot.yazb.model.user.ZhuboDetailModel;
import com.flot.yazb.widget.utils.GlideImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

public class MediaPreviewActivity extends BActivity {

    private ArrayList<ZhuboDetailModel.ImgsModel> images = new ArrayList<>();
    private int currentIndex = 0;
    private ViewPager viewPager;
    private PhotoPagerAdapter adapter;
    private int index = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_picker_media_preview);

        index = getIntent().getIntExtra("index", 0);
        images = (ArrayList<ZhuboDetailModel.ImgsModel>) getIntent().getSerializableExtra("data");

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        adapter = new PhotoPagerAdapter(MediaPreviewActivity.this, images);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(currentIndex);
        viewPager.setOffscreenPageLimit(4);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        viewPager.setCurrentItem(index);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    public class PhotoPagerAdapter extends PagerAdapter {

        private List<ZhuboDetailModel.ImgsModel> images = new ArrayList<>();
        private Context mContext;
        private LayoutInflater mLayoutInflater;

        public PhotoPagerAdapter(Context mContext, List<ZhuboDetailModel.ImgsModel> images) {
            this.mContext = mContext;
            this.images = images;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = mLayoutInflater.inflate(R.layout.image_picker_page_layout, container, false);
            final String uri = images.get(position).thumb;
            TouchImageView imageView = (TouchImageView) itemView.findViewById(R.id.touch_image_view);
            GlideImageLoaderUtil.loadImage(imageView, uri,R.mipmap.activity_splash_background);
            container.addView(itemView);
            return itemView;
        }

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

    }
}
